package com.baichuan.util;

import com.baichuan.entity.AppUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.Map;
import java.util.TreeMap;

/**
 * @Auther: leo
 * @Date: 2020/04/10 21:35
 * @Description: 请求工具类
 */
@Slf4j
public class RequestParamsUtil {
    private static String USER_TOKEN = "_t";
    private static String APP_VERSION = "_v";
    private static String CLIENT_MODEL = "_om";
    private static String CLIENT_OS_VERSION = "_ov";

    /**
     * 头信息转为头对象
     *
     * @return
     */
    public static RequestHeaderParams getHeaderParams() {
        if (RequestContextHolder.getRequestAttributes() == null)
            return null;
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        if (request != null) {
            RequestHeaderParams headerParams = new RequestHeaderParams();
            headerParams.set_t(request.getHeader(USER_TOKEN));
            headerParams.set_v(request.getHeader(APP_VERSION));
            headerParams.set_om(request.getHeader(CLIENT_MODEL));
            headerParams.set_ov(request.getHeader(CLIENT_OS_VERSION));
            return headerParams;
        }
        return null;
    }

    /**
     * 头参数合法性验证
     *
     * @param params
     * @return
     */
    public static boolean verifyHeaderParams(RequestHeaderParams params) {
        if (StringUtils.isEmpty(params.get_v(), params.get_om(), params.get_ov())) {
            return false;
        }
        return true;
    }

    public static String getToken() {
        return getToken(null);
    }

    public static String getToken(HttpServletRequest request) {
        if (request == null && RequestContextHolder.getRequestAttributes() != null)
            request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        if (request == null)
            return null;
        return request.getHeader(USER_TOKEN);
    }

    public static String getOpenId() {
        return getOpenId(null);
    }

    public static String getOpenId(HttpServletRequest request) {
        String token = getToken(request);
        if (token == null)
            return null;
        return AppletUserTokenUtil.getOpenId(token);
    }

    public static Integer getUserId() {
        return getUserId(null);
    }

    public static Integer getUserId(HttpServletRequest request) {
        String token = getToken(request);
        if (token == null)
            return null;
        return AppletUserTokenUtil.getUserId(token);
    }

    public static Integer getSysUserId() {
        return getSysUserId(null);
    }

    public static Integer getSysUserId(HttpServletRequest request) {
        String token = getToken(request);
        if (token == null)
            return null;
        return CmsUserTokenUtil.getUserId(token);
    }

    public static Integer getDealerId() {
        return getDealerId(null);
    }

    public static Integer getDealerId(HttpServletRequest request) {
        String token = getToken(request);
        if (token == null)
            return null;
        return AppletUserTokenUtil.getDealerId(token);
    }

    public static Integer getCustmagerId() {
        return getCustmagerId(null);
    }

    public static Integer getCustmagerId(HttpServletRequest request) {
        String token = getToken(request);
        if (token == null)
            return null;
        Integer custmagerId = AppletUserTokenUtil.getCustMagerId(token);
        if (custmagerId != null && custmagerId == -1)
            custmagerId = null;
        return custmagerId;
    }

    public static AppUser getUser() {
        return getUser(null);
    }

    public static AppUser getUser(HttpServletRequest request) {
        String token = getToken(request);
        if (token == null)
            return null;
        return AppletUserTokenUtil.getUser(token);
    }

    public static Map getRequestParams() {
        if (RequestContextHolder.getRequestAttributes() == null)
            return null;
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        if (request != null) {
            Map<String, String> res = new TreeMap<>();
            Enumeration<?> temp = request.getParameterNames();
            if (null != temp) {
                while (temp.hasMoreElements()) {
                    String en = (String) temp.nextElement();
                    String value = request.getParameter(en);
                    res.put(en, value);
                }
            }
            return res;
        }
        return null;
    }
}

